package com.agile.major.web.rest;

import com.agile.major.domain.LastGraduateEmployment;
import com.agile.major.service.LastGraduateEmploymentService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing LastGraduateEmployment.
 */
@RestController
@RequestMapping("/api")
public class LastGraduateEmploymentResource {

    private final Logger log = LoggerFactory.getLogger(LastGraduateEmploymentResource.class);

    private static final String ENTITY_NAME = "lastGraduateEmployment";

    private final LastGraduateEmploymentService lastGraduateEmploymentService;

    public LastGraduateEmploymentResource(LastGraduateEmploymentService lastGraduateEmploymentService) {
        this.lastGraduateEmploymentService = lastGraduateEmploymentService;
    }

    /**
     * POST  /last-graduate-employments : Create a new lastGraduateEmployment.
     *
     * @param lastGraduateEmployment the lastGraduateEmployment to create
     * @return the ResponseEntity with status 201 (Created) and with body the new lastGraduateEmployment, or with status 400 (Bad Request) if the lastGraduateEmployment has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/last-graduate-employments")
    @Timed
    public ResponseEntity<LastGraduateEmployment> createLastGraduateEmployment(@RequestBody LastGraduateEmployment lastGraduateEmployment) throws URISyntaxException {
        log.debug("REST request to save LastGraduateEmployment : {}", lastGraduateEmployment);
        if (lastGraduateEmployment.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new lastGraduateEmployment cannot already have an ID")).body(null);
        }
        LastGraduateEmployment result = lastGraduateEmploymentService.save(lastGraduateEmployment);
        return ResponseEntity.created(new URI("/api/last-graduate-employments/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /last-graduate-employments : Updates an existing lastGraduateEmployment.
     *
     * @param lastGraduateEmployment the lastGraduateEmployment to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated lastGraduateEmployment,
     * or with status 400 (Bad Request) if the lastGraduateEmployment is not valid,
     * or with status 500 (Internal Server Error) if the lastGraduateEmployment couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/last-graduate-employments")
    @Timed
    public ResponseEntity<LastGraduateEmployment> updateLastGraduateEmployment(@RequestBody LastGraduateEmployment lastGraduateEmployment) throws URISyntaxException {
        log.debug("REST request to update LastGraduateEmployment : {}", lastGraduateEmployment);
        if (lastGraduateEmployment.getId() == null) {
            return createLastGraduateEmployment(lastGraduateEmployment);
        }
        LastGraduateEmployment result = lastGraduateEmploymentService.save(lastGraduateEmployment);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, lastGraduateEmployment.getId().toString()))
            .body(result);
    }

    /**
     * GET  /last-graduate-employments : get all the lastGraduateEmployments.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of lastGraduateEmployments in body
     */
    @GetMapping("/last-graduate-employments")
    @Timed
    public ResponseEntity<List<LastGraduateEmployment>> getAllLastGraduateEmployments(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of LastGraduateEmployments");
        Page<LastGraduateEmployment> page = lastGraduateEmploymentService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/last-graduate-employments");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /last-graduate-employments/:id : get the "id" lastGraduateEmployment.
     *
     * @param id the id of the lastGraduateEmployment to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the lastGraduateEmployment, or with status 404 (Not Found)
     */
    @GetMapping("/last-graduate-employments/{id}")
    @Timed
    public ResponseEntity<LastGraduateEmployment> getLastGraduateEmployment(@PathVariable Long id) {
        log.debug("REST request to get LastGraduateEmployment : {}", id);
        LastGraduateEmployment lastGraduateEmployment = lastGraduateEmploymentService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(lastGraduateEmployment));
    }

    /**
     * DELETE  /last-graduate-employments/:id : delete the "id" lastGraduateEmployment.
     *
     * @param id the id of the lastGraduateEmployment to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/last-graduate-employments/{id}")
    @Timed
    public ResponseEntity<Void> deleteLastGraduateEmployment(@PathVariable Long id) {
        log.debug("REST request to delete LastGraduateEmployment : {}", id);
        lastGraduateEmploymentService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    @GetMapping("/last-graduate-employments/{baseMajorId}/{year}")
    @Timed
    public ResponseEntity<List<LastGraduateEmployment>> getLastGraduateStatus(@PathVariable Long baseMajorId,@PathVariable String year) {
        List<LastGraduateEmployment> result = lastGraduateEmploymentService.getLastGraduateEmploymentByMajorAndYear(baseMajorId,year);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
